Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compiler diagnostic when HandlerFn is not implemented #125

Merged
merged 1 commit into from
May 4, 2024

Conversation

progval
Copy link
Collaborator

@progval progval commented May 4, 2024

For example, if I add a u64 parameter to a handler, the compiler now reports this:

error[E0277]: Invalid command handler
  --> sable_ircd/src/command/handlers/who.rs:5:1
   |
5  | #[command_handler("WHO")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}` is not a valid command handler
   |
   = help: the trait `handler::HandlerFn<'_, _, _>` is not implemented for fn item `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}`
   = note: All parameter types must implement `AmbientArgument` or `PositionalArgument`
   = note: Return type must be `CommandResult`
note: required by a bound in `call_handler`
  --> sable_ircd/src/command/plumbing/mod.rs:47:20
   |
45 | pub(crate) fn call_handler<'a, Amb, Pos>(
   |               ------------ required by a bound in this function
46 |     ctx: &'a dyn Command,
47 |     handler: &impl HandlerFn<'a, Amb, Pos>,
   |                    ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `call_handler`
   = note: this error originates in the attribute macro `command_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

instead of this:

error[E0277]: the trait bound `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}: handler::HandlerFn<'_, _, _>` is not satisfied
  --> sable_ircd/src/command/handlers/who.rs:5:1
   |
5  | #[command_handler("WHO")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `handler::HandlerFn<'_, _, _>` is not implemented for fn item `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}`
   |
note: required by a bound in `call_handler`
  --> sable_ircd/src/command/plumbing/mod.rs:47:20
   |
45 | pub(crate) fn call_handler<'a, Amb, Pos>(
   |               ------------ required by a bound in this function
46 |     ctx: &'a dyn Command,
47 |     handler: &impl HandlerFn<'a, Amb, Pos>,
   |                    ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `call_handler`
   = note: this error originates in the attribute macro `command_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

This uses the diagnostic::on_unimplemented attribute stabilized by Rust 1.78

I believe it can make it a little easier for people getting started with the codebase. What do you think?

For example, if I add a `u64` parameter to a handler, the compiler now
reports this:

```
error[E0277]: Invalid command handler
  --> sable_ircd/src/command/handlers/who.rs:5:1
   |
5  | #[command_handler("WHO")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}` is not a valid command handler
   |
   = help: the trait `handler::HandlerFn<'_, _, _>` is not implemented for fn item `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}`
   = note: All parameter types must implement `AmbientArgument` or `PositionalArgument`
   = note: Return type must be `CommandResult`
note: required by a bound in `call_handler`
  --> sable_ircd/src/command/plumbing/mod.rs:47:20
   |
45 | pub(crate) fn call_handler<'a, Amb, Pos>(
   |               ------------ required by a bound in this function
46 |     ctx: &'a dyn Command,
47 |     handler: &impl HandlerFn<'a, Amb, Pos>,
   |                    ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `call_handler`
   = note: this error originates in the attribute macro `command_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
```

instead of this:

```
error[E0277]: the trait bound `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}: handler::HandlerFn<'_, _, _>` is not satisfied
  --> sable_ircd/src/command/handlers/who.rs:5:1
   |
5  | #[command_handler("WHO")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `handler::HandlerFn<'_, _, _>` is not implemented for fn item `for<'a, 'b, 'c, 'd, 'e> fn(&'a server::ClientServer, &'b sable_network::prelude::Network, &'c (dyn command_response::CommandResponse + 'c), source_types::UserSource<'d>, &'e str, u64) -> Result<(), command::error::CommandError> {handle_who}`
   |
note: required by a bound in `call_handler`
  --> sable_ircd/src/command/plumbing/mod.rs:47:20
   |
45 | pub(crate) fn call_handler<'a, Amb, Pos>(
   |               ------------ required by a bound in this function
46 |     ctx: &'a dyn Command,
47 |     handler: &impl HandlerFn<'a, Amb, Pos>,
   |                    ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `call_handler`
   = note: this error originates in the attribute macro `command_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
```

This uses the `diagnostic::on_unimplemented` attribute stabilized by
[Rust 1.78](https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html)
@spb spb merged commit ee0a9cc into Libera-Chat:master May 4, 2024
2 checks passed
@progval progval deleted the handler-diagnostic branch May 4, 2024 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants